package com.gjt.zyglxt.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.gjt.zyglxt.dao.IConsumeDao;
import com.gjt.zyglxt.dao.IMemberDao;
import com.gjt.zyglxt.dto.ConsumeDTO;
import com.gjt.zyglxt.model.Consume;
import com.gjt.zyglxt.model.Member;

import javax.persistence.criteria.Expression;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

@Service
public class ConsumeServiceImp implements IConsumeService {

    @Resource
    private IMemberDao memberDao;
    @Resource
    private IConsumeDao consumDao;

    @Transactional
    @Override
    public Consume swipingCard(Integer readRoomId, String cardNo, boolean inOrOut) {
        // 根据卡号获取会员对象
        Member m = memberDao.getCardStatus(cardNo);
        Consume log = null;
        if (inOrOut) {// 如果是刷卡进入
            if (m.getStatus() == 0) {// 状态正常
                Date date = new Date();
                // 记录刷卡时间
                log = new Consume();
                log.setCardNo(cardNo);
                log.setInTime(date);
                log.setReadRoomId(readRoomId);
                // 设置离开时间为进入时间+30分钟
                Calendar nowTime = Calendar.getInstance();
                nowTime.add(Calendar.MINUTE, 30);
                log.setOutTime(nowTime.getTime());
                log = consumDao.save(log);
                // 修改会员卡状态为资源室id
                m.setStatus(readRoomId);
                memberDao.save(m);
            } else {
                // 状态异常，联系管理员
                System.out.println("会员状态异常，请联系管理员");

            }

        } else {// 如果是刷卡离开
                // 查询会员最后一次进入资源室的日志编号
            log = consumDao.getMaxInTime(cardNo);
            // 更新离开时间
            consumDao.updateOutTime(log.getId(), new Date());
            // 修改会员卡状态为0
            m.setStatus(0);

        }
        return log;// 返回Cousume对象
    }

    @Override
    public Page<Consume> findPage(ConsumeDTO dto) {
        Sort sort = Sort.by(Sort.Order.asc("id"));
        PageRequest page = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), sort);
        return consumDao.findAll(getWhereClause(dto), page);

    }

    private Specification<Consume> getWhereClause(final ConsumeDTO dto) {
        return new Specification<Consume>(){

			/**
             *
             */
            private static final long serialVersionUID = 1L;

            @Override
			public Predicate toPredicate(Root<Consume> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.conjunction();//动态SQL查询
               List<Expression<Boolean>> exList = p.getExpressions();

               if(dto.getCardNo()!=null&&!"".equals(dto.getCardNo())){
                   exList.add(criteriaBuilder.equal(root.<String>get("cardNo"),dto.getCardNo()));
               }
               if (dto.getStartDate()!=null) {
                exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("inTime"), dto.getStartDate()));
               }
               if (dto.getEndDate()!=null) {
                    exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("outTime"), dto.getEndDate()));
               }
              
				return p;
			}

        };
        
    }

}